package com.xywy.common;

import java.lang.ref.WeakReference;

import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.PopupWindow;

import com.umeng.analytics.MobclickAgent;
import com.xywy.R;
import com.xywy.common.app.MainApplication;
import com.xywy.common.constants.ErrorCode;
import com.xywy.common.util.MLog;
import com.xywy.common.widget.TitleBar;

/**
 * 名称: BaseActivity 描述: 所有Activity的基类 最近修改时间:2014-04-24 9:10
 * 
 * @author xgd
 * @since 2014-04-24
 */
public abstract class BaseActivity extends FragmentActivity implements
		NetWorkReceiver.NetWorkListener {
	public static final String TAG = "BaseActivity";
	protected NetWorkReceiver receiver;
	public MainApplication mApp;
	protected WeakReference<Context> mContext;
	protected Context context;
	public ProgressDialog dialog;
	private TitleBar titleBar;
	public Dialog errDialog;
	private PopupWindow no_net_pop;
	/**
	 * 页面是否存活判断，避免dialog 的show出异常
	 */
	public boolean active;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		receiver = new NetWorkReceiver();
		receiver.setNetWorkListener(this);
		mContext = new WeakReference<Context>(this);
		context = mContext.get();
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	/**
	 * 预初始化，接收从上个页面中传递过来的数据
	 */
	public abstract void preInit();

	/**
	 * 加载页面数据
	 */
	public abstract void loadData();

	/**
	 * 初始化布局文件中的控件
	 */
	public abstract void initView();

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 建立持有activity弱引用的handler
	 */
	protected static class BaseHandler extends Handler {
		WeakReference<Activity> mActivity;

		protected BaseHandler(Activity activity) {
			mActivity = new WeakReference<Activity>(activity);
		}
	}

	/**
	 * 通过方法拿到创建 handler对应的activity
	 */
	private Handler netWorkHandler = new BaseHandler(this) {

		public void handleMessage(Message msg) {

			if (msg.what == ErrorCode.ERROR) {
				if (dialog != null && dialog.isShowing()) {
					dialog.dismiss();
				}
				msg.obj = null;
			}

			try {
				BaseActivity.this.handleMessage(msg);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	};

	/**
	 * 代替handler中的handleMessage，在子类中使用
	 */
	protected void handleMessage(Message msg) throws JSONException {
		if (msg.obj == null) {
			return;
		}
	}

	/**
	 * 提供给BaseTask 类
	 * 
	 * @return
	 */
	public Handler getNetWorkHandler() {
		return netWorkHandler;
	}

	@Override
	protected void onStart() {
		super.onStart();
		active = true;
		MLog.e(TAG, "创建了 ");
		MainApplication.add(this);
	}

	@Override
	protected void onStop() {
		active = false;
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		MLog.e(TAG, "销毁了");
		MainApplication.remove(this);
		super.onDestroy();
	}

	@Override
	public void hasNetwork() {
		dismissNoNetwork();
		if (null != errDialog) {
			errDialog.dismiss();
		}
	}

	/**
	 * 网络异常popupwindow消失
	 * 
	 * @return
	 */
	protected boolean dismissNoNetwork() {
		boolean b = false;
		if (null != no_net_pop && no_net_pop.isShowing()) {
			no_net_pop.dismiss();
			b = true;
		}
		if (errDialog != null && errDialog.isShowing()) {
			errDialog.dismiss();
			b = true;
		}
		return b;
	}

	@Override
	public void noNetwork() {
		titleBar = (TitleBar) findViewById(R.id.title_bar);
		if (titleBar == null) {
			errDialog = new AlertDialog.Builder(this).setTitle("网络异常")
					.setPositiveButton("确认", null).create();
			errDialog.show();
			MLog.e(TAG, "网络异常");
		}
	}

//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {

//		if (keyCode == KeyEvent.KEYCODE_HOME) {
//			System.out.println("点击了home按键；");
//		}
//		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			System.out.println("点击了back按键；");
//		}

//		return true;
//	}

}
